$(document).ready(function() {
	var testObj = null;
	var cliknum = true;
	//选择商品
	$('.qz_select').click(function(){
		testObj = $(this);
		var $checkbox=$(this).find('input[type=checkbox]');

		//点击其他选项时去除默认选项
		if(cliknum == true && testObj.parent().index() != 0){
			$('input[name=goods]:first').parents('.qz_select:first').removeClass('qz_cur');
			$('input[name=goods]:first').prop('checked',false);
			cliknum = false;
		}

		if(!$checkbox.is(":checked")){
			$(this).addClass('qz_cur');
			$checkbox.prop('checked',true);
			$(this).parent().css('border','2px solid #f00');
		}else{
			$(this).removeClass('qz_cur');
			$checkbox.prop('checked',false);
			$(this).parent().css('border','none');
			$(this).parent().css('border-bottom','1px solid #d8d8d8');
		}
		
		compute_price();	
	});
	$(".pixiu_num").click(function(){
		$(this).parent().prev().click();
	});
	$(".list_b").click(function(){
		$(this).parents(".order_list").find(".qz_select").click();	
	})
	//默认勾选产品
	$('input[name=goods]:first').parents('.qz_select:first').click();

	//选择商品数量
	$('.goods_num').click(function(){
		var gid = $(this).attr('gid'),
			goods_num = $("#goods_"+gid+"_num"),
			num = parseInt(goods_num.text()),
			$checkbox = $('input[name=goods][value='+gid+']');
			// $checkbox = $('input[name=goods][data='+gid+']');

		if(!$checkbox.is(":checked")) {
			$checkbox.parents('.qz_select:first').click();
		}

		if($(this).attr('act') == '-'){
			if( num <= 1 ){			
				compute_price();						
				return false;		
			}
			num = num - 1;
		}else{
			if(num >= 100){
				get_tck('您确定购买这么多产品？请联系客服！');
				return false;
			}
			num = num + 1;
			if(gid == '1360'){
				if(num > '5'){
					num = 5;
				}	
			}
		}
		
		goods_num.text(num);
		compute_price();
	});
	function getGoodsNum() {
		var Rand = Math.random()*10;
		if(Rand < 5){
			return '1364';
		} else {
			return '1375';
		}
	}

	//新系列订单提交方法
	$('#buy_submit').click(function (){
		//var category= $('input[name="goods"]:checked').parents(".order_list").find(".shuxiang");
		var $btn_submit = $(this);
		if($btn_submit.attr("disabled")) {
			return false;
		}
		var data = {},
			zeng = 0,
			zNum = ['1388','1387','1386','1375'],
			cart = [];

		data.consignee = $.trim($('input[name="name"]').val());//客户姓名
		data.year = $('select[name="sel_year"] option:selected').val();//年
		data.month = $('select[name="sel_month"] option:selected').val();//月
		data.day = $('select[name="sel_day"] option:selected').val();//日
		data.mobile = $.trim($('input[name="mob"]').val());//联系手机
		data.tel = '';//固定电话
		data.province = $('select[name="s_province"] option:selected').val();//省
		data.city = $('select[name="s_city"] option:selected').val();//市
		data.area = $('select[name="s_county"] option:selected').val();//区
		data.address = $.trim($('input[name="address"]').val());//详细地址
		data.postscript = $.trim($('textarea[name="guest"]').val());//客户留言
		data.pay_id = 6;//付款方式
		//data.email = data.year+data.month+data.day; //出生年月日字符串
		var sizeInput = $('input[name="goods"]:checked').size();

		$('input[name="goods"]:checked').each(function (){
			var gd_id = $(this).val(),
			//gd_type = $(this).attr('data'),
			//count = parseInt($("#goods_"+gd_type+"_num").text());
			count = parseInt($("#goods_"+gd_id+"_num").text());
			//赠品
			if(sizeInput>1){
				if(gd_id != '1360'){
					zeng += parseInt(count);
				}
				// if(gd_type != 'g'){
				// 	zeng += parseInt(count);
				// }
			}else if(sizeInput == 1){
				zeng += parseInt(count);
			}
			
			cart[cart.length] = {'gd_id': gd_id, 'count': count};
		});
		//赠品
		if(zeng != 0){
			$.each(zNum,function(index,value){
				cart[cart.length] = {'gd_id':value, 'count':zeng};
			})
		}
		
		console.log(cart);
		data.cart = JSON.stringify(cart);
				
		if(cart.length <= 0){
			get_tck('请选择商品');
			return false;
		}
		
		// if(category.text() == '请选择属相'){
		// 	category.click();
		// 	return false;
		// }

		if(data.consignee.length < 2){
			get_tck('请核对收货人');
			return false;
		}
		if(data.year == '选年份'){
			get_tck('请核对出生年份');
			return false;
		}
		if(data.month == '选月份'){
			get_tck('请核对出生月份');
			return false;
		}
		if(data.day == '选日期'){
			get_tck('请核对出生日期');
			return false;
		}
		if(!/^1[34578]{1}[0-9]{9}$/.test(data.mobile)){
			get_tck('请核对手机号码');
			return false;
		}
		if(data.province == '选省份'){
			get_tck('请核对省份地址');
			return false;
		}
		if(data.city == '选城市'){
			get_tck('请核对城市地址');
			return false;
		}
		// if(data.area == '选地区'){
		// 	get_tck('请核对收货地址');
		// 	return false;
		// }
		if(data.address.length < 2){
		   get_tck('请核对收货详细地址');
			return false;
		}

		$.ajax({
			url: 'http://api.cogiskin.com/index.php/Open/Order/submit',
			dataType: 'jsonp',
			type: 'get',
			data: data,
			jsonp: 'callback',
			beforeSend: function() {
				$btn_submit.attr("disabled", true);
			},
			complete: function() {
				$btn_submit.removeAttr("disabled");
			},
			success: function(result) {
				var msg = '', 
					pay_info = '';
				if (result.status == 1) {
					msg = '<dl style="width: 90%;margin: 0 auto;text-align: center;color: #333;padding: 1rem 0;border: 8px double #f66;background: #f4e8d0;"><dt style="font-size:20px;color:#333"><img src="img/suc.png" style="margin-right:5px; width:24px; height:19px;">'+result.info+'</dt><dd style="margin:0;line-height:180%;padding:0 0.8rem;font-size:17px;">亲，您将收到 020-627207** 来电，<span style="color:#f00;">（9:00~18:30的订单，客服会在半个小时内与您联系）</span>，为您核实订单信息，敬请留意接听。</dd></dl>';

					$('input[name="mob"]').val(''); //清空电话
					$('input[name="name"]').val(''); //清空收货人
					$('input[name="address"]').val(''); //清空地址
					$('textarea[name="guest"]').val('');//清空留言
					_init_area();
					
					ga('set', 'dimension1', data.mobile);
					ga('set', 'metric1', result.data);
					ga('send', 'pageview', {'page': '/Online_wenwan_jdft','title': '提交在线订单'});
					sa.send({type: 'Article_Copy', argument:'wenwan/jdft', content: 'submitAll'});
					
				}else{
					 msg = result.info + '<br/><input type="button" class="return_btn" onclick="closebox();" value="确定" />';
				}

				$('#order_return').html(msg);
			}
		});
	});
	
});
//计算价格
function compute_price(){
	var data = {},
		cart = [];
		
	$('input[name="goods"]:checked').each(function (){
		var gd_id = $(this).val(),
			//gd_type = $(this).attr('data'),
			//count = parseInt($("#goods_"+gd_type+"_num").text());
		count = parseInt($("#goods_"+gd_id+"_num").text());

		cart[cart.length] = {'gd_id': gd_id, 'count': count};
	});
	data.cart = JSON.stringify(cart);
	
	$.ajax({
		url: 'http://api.cogiskin.com/index.php/Open/Order/calc',
		dataType: 'jsonp',
		type: 'get',
		data: data,
		jsonp: 'callback',
		success: function(result) {
			$('#total-price').val(result.goods_total);
			$('#freight').text(result.freight);
			if(result.freight == '0.00') {
				$('#freight-info').text('免运费');
			} else {
				$('#freight-info').text('含运费');
			}
			
			$('#final-price').html(result.total);
		}
	});
}

//提示
function get_tck(text) {
	var msg='<div class="tck_list"><div class="tck"><dl><dt><a href="javascript:;" onclick="tck_cose()"><img src="img/xx.jpg" width="14"></a></dt><dd><img class="tck-put" src="img/gant.jpg" width="16"><span id="tck_text">'+text+'</span></dd></dl></div></div>';
	$("#tck_m").html(msg);
	$("#tck_m").show();
}

//关闭提示
function tck_cose() {
	$('#tck_m').hide();	
}


function checkbrowse() {   
	var ua = navigator.userAgent.toLowerCase();   
	var is = (ua.match(/\b(chrome|opera|safari|msie|firefox)\b/) || ['', 'mozilla'])[1];   
	var r = '(?:' + is + '|version)[\\/: ]([\\d.]+)';   
	var v = (ua.match(new RegExp(r)) || [])[1];   
	jQuery.browser.is = is;   
	jQuery.browser.ver = v;   
	return {   
	'is': jQuery.browser.is,   
	'ver': jQuery.browser.ver   
	}   
}

function setSales() {
	var timestamp = parseInt(Date.parse(new Date())) / 1000,
		num = Math.ceil(timestamp / 3600),
		num_1 = num.toString().substring(1, 2),
		num_2 = num.toString().substring(2, 6);
	num_1 = parseInt(num_1) + 1;
	num = Math.ceil(parseInt(num_1.toString() + num_2) * 1.05)+3000;
	return num;
}